﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using Knihotoc_WP8.Settings;

namespace Knihotoc_WP8.CustomControls
{
    public partial class UnregisteredUserData : UserControl
    {
        public UnregisteredUserData()
        {
            this.InitializeComponent();
        }

        private void LoginButton_Click(object sender, RoutedEventArgs e)
        {
            string test = "test";
            byte[] saltByte = Encoding.UTF8.GetBytes("somesalt");

            string passwordEncrypted;

            using (Aes aes = new AesManaged())
            {
                Rfc2898DeriveBytes deriveBytes = new Rfc2898DeriveBytes(test, saltByte);
                aes.Key = deriveBytes.GetBytes(128 / 8);
                aes.IV = aes.Key;
                using (MemoryStream encryptionStream = new MemoryStream())
                {
                    using (CryptoStream encrypt = new CryptoStream(encryptionStream, aes.CreateEncryptor(), CryptoStreamMode.Write))
                    {
                        byte[] passwordBytes = UTF8Encoding.UTF8.GetBytes(this.passwordText.Password);
                        encrypt.Write(passwordBytes, 0, passwordBytes.Length);
                        encrypt.FlushFinalBlock();
                    }
                    passwordEncrypted = Convert.ToBase64String(encryptionStream.ToArray());
                }
            }

            //prihlasenie uzivatela
            Data.User testUser = new Data.User();
            testUser.FirstName = "Milan";
            testUser.SurName = "Nowak";
            testUser.UserName = "milanko";
            testUser.TotalFound = 26;
            testUser.TotalBorrowed = 11;
            testUser.ID = 64;
            testUser.Password = "bla";
            CurrentSettings.Instance.User = testUser;
            this.UserLoggedIn(this, null);

        }

        public event EventHandler UserLoggedIn;

        private void RegisterButton_Click(object sender, RoutedEventArgs e)
        {

        }

        private void ForgottenPasswordButton_Click(object sender, RoutedEventArgs e)
        {

        }
    }
}
